package com.unisound.tracelog.client.core.listener;

import com.unisound.tracelog.client.core.properties.TraceLogClientProperties;
import com.unisound.tracelog.common.model.span.Span;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogTraceLogListener implements TraceLogListener {
    private static final Logger LOGGER = LoggerFactory.getLogger(LogTraceLogListener.class);

    private TraceLogClientProperties traceLogClientProperties;

    public LogTraceLogListener(TraceLogClientProperties traceLogClientProperties) {
        this.traceLogClientProperties = traceLogClientProperties;
    }

    public void listenerSpanEvent(SpanEvent spanEvent) {
        if (!traceLogClientProperties.isLogEnable()) return;

        if (spanEvent == null) return;
        Span span = spanEvent.getSpan();
        if (span == null) return;

        if (span.isInput()) {
            LOGGER.info("[{}]【{}】 start. 请求:{}", span.getTraceId(), span.getName(), span.getContent());
        } else {
            LOGGER.info("[{}]【{}】 end. 耗时(ms):{} 结果:{}", span.getTraceId(), span.getName(), span.getElapsedTimeMillis(), span.getContent());
        }
    }

    @Override
    public void listenerTagEvent(TagEvent tagEvent) {

    }

}
